package MAgentAssignment;

import MAgentAssignment.gui.InterfaceAgent;
import MAgentAssignment.ontology.FlightOntology;
import MAgentAssignment.ontology.FlightVocabulary;
import MAgentAssignment.ontology.actions.CreateAirpot;
import MAgentAssignment.ontology.actions.CreateNewFlight;
import MAgentAssignment.ontology.actions.Land;
import MAgentAssignment.ontology.concepts.Airport;
import MAgentAssignment.ontology.concepts.Flight;
import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: prageeth
 * Date: 2/16/12
 * Time: 2:25 PM
 * To change this template use File | Settings | File Templates.
 */
public class AirportManagerAgent extends Agent
{
    private Codec codec = new SLCodec();
    private Ontology ontology = FlightOntology.getInstance();
    private Airport airport;
    private AID[] monitorAgents;

    @Override protected void setup()
    {

        getContentManager().registerLanguage( codec );
        getContentManager().registerOntology( ontology );
        monitorAgents = new AID[]{new AID( InterfaceAgent.agentName, AID.ISLOCALNAME )};
        ServiceDescription sd = new ServiceDescription();
        sd.setType( "AIRPORT" );
        sd.setName( getLocalName() );
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName( getAID() );
        dfd.addServices( sd );
        try
        {
            DFService.register( this, dfd );
        }
        catch( FIPAException fe )
        {
            fe.printStackTrace();
        }

        addBehaviour( new CyclicBehaviour()
        {
            @Override public void action()
            {
                ACLMessage msg = receive( MessageTemplate.MatchPerformative( ACLMessage.INFORM ) );

                if( msg != null )
                {
                    if( msg.getContent() != null )
                    {
                        if( msg.getContent().equals( FlightVocabulary.AIRPORT_CLOSED ) )
                        {
                            airport = null;
                            block();
                        }


                    }

                    Object contentObj = null;
                    try
                    {
                        contentObj = msg.getContentObject();
                    }
                    catch( UnreadableException e )
                    {
                        e.printStackTrace();
                    }
                    if( contentObj != null )
                    {
                        if( contentObj instanceof Flight )
                        {
                            Flight flight = (Flight) contentObj;
                            for( int i = 0; i < airport.getIncomingFlights().size(); i++ )
                            {
                                Flight flight1 = airport.getIncomingFlights().get( i );
                                if( flight1.getFlightNo().equals( flight.getFlightNo() ) )
                                {
                                    airport.getIncomingFlights().remove( flight1 );
                                    airport.getStayFlight().add( flight1 );
                                    i--;
                                }
                            }
                        }
                        else if( contentObj instanceof CreateNewFlight )
                        {
                            CreateNewFlight createNewFlight = (CreateNewFlight) contentObj;
                            if( createNewFlight.getFlight().getFromAirport().getAirpotCode().equals( airport.getAirpotCode() ) )
                            {
                                airport.getStayFlight().remove( createNewFlight.getFlight() );
                            }
                            else if( createNewFlight.getFlight().getToAirport().getAirpotCode().equals( airport.getAirpotCode() ) )
                            {
                                airport.getIncomingFlights().add( createNewFlight.getFlight() );
                            }
                        }
                        else if( contentObj instanceof CreateAirpot )
                        {
                            CreateAirpot createAirpot = (CreateAirpot) contentObj;
                            airport = createAirpot.getAirport();
                            addBehaviour( new TickerBehaviour( myAgent, 1000 )
                            {
                                @Override protected void onTick()
                                {
                                    ACLMessage msg = new ACLMessage( ACLMessage.INFORM );
                                    msg.setLanguage( codec.getName() );
                                    try
                                    {
                                        CreateAirpot createAirpot = new CreateAirpot();
                                        createAirpot.setAirport( airport );
                                        msg.setContentObject( createAirpot );
                                    }
                                    catch( IOException e )
                                    {
                                        e.printStackTrace();
                                    }

                                    msg.addReceiver( new AID( InterfaceAgent.agentName, AID.ISLOCALNAME ) );

                                    send( msg );
                                    stop();
                                }
                            } );
                        }
                    }
                }
                else
                {
                    msg = receive( MessageTemplate.MatchPerformative( ACLMessage.REQUEST ) );
                    if( msg != null && msg.getContent() != null && msg.getContent().equals( FlightVocabulary.WHAT_IS_THE_AIRPORT ) )
                    {
                        ACLMessage aclMessage = msg.createReply();
                        aclMessage.setPerformative( ACLMessage.INFORM );
                        CreateAirpot createAirpot = new CreateAirpot();
                        createAirpot.setAirport( airport );
                        try
                        {
                            aclMessage.setContentObject( createAirpot );
                        }
                        catch( IOException e )
                        {
                            e.printStackTrace();
                        }
                        send( aclMessage );

                    }
                }

            }
        } );
    }

    public Airport getAirport()
    {
        return airport;
    }

    public void setAirport( Airport airport )
    {
        this.airport = airport;
    }
}
